package com.liang.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liang.pojo.BoyFriend;
import com.liang.pojo.GirlFriend;
import com.liang.service.BoyFriendService;
import com.liang.service.GirlFriendService;

@Controller
public class GirlFriendController {
	@Autowired
	@Qualifier("girlFriendServiceImpl")
	private GirlFriendService girlFriendService;
	@Autowired
	@Qualifier("boyFriendServiceImpl")
	private BoyFriendService boyFriendService;
	
	
	@RequestMapping("/selectGirlFriendList")
	public String selectGirlFriendList(Model model) {
		List<GirlFriend> girlFriendList = girlFriendService.selectGirlFriendList();
		if(girlFriendList!=null) {
			model.addAttribute("girlFriends", girlFriendList);
			return "girlFriend";
		}
		model.addAttribute("msg", "女朋友表里数据为空");
		return "error/error";
	}
	
	@RequestMapping("/selectGirlFriendByid")
	public String selectGirlFriendByid(int gid,Model model) {
		GirlFriend girlFriend = girlFriendService.selectGirlFriendByid(gid);
		if(girlFriend!=null) {
			List<GirlFriend> list=new ArrayList<GirlFriend>();
			list.add(girlFriend);
			model.addAttribute("girlFriends", list);
			return "girlFriend";
		}
		model.addAttribute("msg", "根据id查不到对应的女朋友");
		return "error/error";
	}
	@RequestMapping("/insertGirlFriend")
	public String insertGirlFriend(GirlFriend girlFriend,Model model,HttpSession session) {
		GirlFriend girlFriend2 = girlFriendService.selectGirlFriendByid(girlFriend.getGid());
		System.out.println("=================");
		System.out.println(girlFriend2==null);
		if(girlFriend2==null) {
			girlFriendService.insertGirlFriend(girlFriend);
			return selectGirlFriendList(model);
		}
			model.addAttribute("username", session.getAttribute("info"));
			model.addAttribute("msg2", "ID已存在");
			return "love";
	}
	@RequestMapping("/toUpdateGirlFriend/{gid}")
	public String toUpdateGirlFriend(@PathVariable("gid")int gid,Model model) {
		GirlFriend girlFriend = girlFriendService.selectGirlFriendByid(gid);
		model.addAttribute("girlFriend", girlFriend);
		return "toUpdateGirlFriend";
	}
	@RequestMapping("/updateGirlFriend")
	public String updateGirlFriend(GirlFriend girlFriend,Model model) {
		girlFriendService.updateGirlFriend(girlFriend);
		return selectGirlFriendList(model);
	}
	@RequestMapping("/deleteGirlFriend/{gid}")
	public String deleteGirlFriend(@PathVariable("gid")int gid,Model model) {
		List<BoyFriend> boyFriendList = boyFriendService.selectBoyFriendList();
		for(int i=0;i<boyFriendList.size();i++) {
			if(boyFriendList.get(i).getId()==gid) {
				model.addAttribute("msg", "名下有男朋友,要先删除男朋友");
				return selectGirlFriendList(model);
			}
		}
		girlFriendService.deleteGirlFriend(gid);
		return selectGirlFriendList(model);
	}
	@RequestMapping("/add")
	public String add(Model model) {
		GirlFriend girlFriend = new GirlFriend();
		for(int i=1;i<=10;i++) {
			/* girlFriend.setGid(9+i); */girlFriend.setGname("女朋友"+i);
			girlFriend.setGage(15+i);girlFriend.setGdetail("女朋友"+i+"号");
			girlFriendService.insertGirlFriend(girlFriend);
		}
		return selectGirlFriendList(model);
	}
}
